package com.imooc.controller;

import com.imooc.utils.RedisOperator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author gcq
 * @Create 2022-01-02
 */
@ApiIgnore // 忽略
@RestController
@RequestMapping("/redis")
public class RedisController {

    final static Logger logger = LoggerFactory.getLogger(RedisController.class);

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private RedisOperator redisOperator;


    @GetMapping("/set")
    public String set(@RequestParam String key, @RequestParam String value) {
        redisOperator.set(key, value);
        return "ok";
    }

    @GetMapping("/get")
    public String get(@RequestParam String key) {
        return redisOperator.get(key);
    }

    @GetMapping("/delete")
    public String del(@RequestParam String key) {
        redisOperator.del(key);
        return "ok";
    }

    /**
     * 大量key查询
     * @param keys
     * @return
     */
    @GetMapping("/mget")
    public Object mget(String... keys) {
        // 循环查询key
//        List<String> list = new ArrayList<>();
//        for (String key : keys) {
//            String str = redisOperator.get(key);
//            list.add(str);
//        }
        // 批量查询
        List<String> keysList = Arrays.asList(keys);
        return redisOperator.mget(keysList);
    }
}